//
//  HealthsViewController.m
//  FreeReading
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "HealthsViewController.h"
#import "HealthModel.h"
#import "HealthCell_01.h"
#import "HealthCell_02.h"
#import "HealthCell_03.h"
#import "HealthCell_04.h"
#import "HealthCell_05.h"
#import "HealthModel.h"
#import "EnjoyDetailViewController.h"

#define kHealth @"http://dis.myzaker.com/api/get_post.php?_appid=iphone&_bsize=750_1334&_dev=iPhone%2C8.4&_idfa=C55EE99A-4D51-4D6F-850F-535F04291BBA&_lat=0.000000&_lng=0.000000&_mac=02%3A00%3A00%3A00%3A00%3A00&_net=wifi&_udid=8C214DA2-AE28-4216-829D-2BF8F286C89E&_uid=&_utoken=&_v=6.4.1&_version=6.4&discussion_id=215&sort_type=last_comment"



@interface HealthsViewController ()
{
    BOOL _isHeadRefreshing;
    NSString *_nextUrl;
}

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation HealthsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"HealthCell_01" bundle:nil] forCellReuseIdentifier:@"healthCell1"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HealthCell_02" bundle:nil] forCellReuseIdentifier:@"healthCell2"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HealthCell_03" bundle:nil] forCellReuseIdentifier:@"healthCell3"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HealthCell_04" bundle:nil] forCellReuseIdentifier:@"healthCell4"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HealthCell_05" bundle:nil] forCellReuseIdentifier:@"healthCell5"];
    
    self.dataArray = [NSMutableArray array];
    
    [self setUpRefresh];
    
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if ([self isViewLoaded] && self.view.window == nil) {
        
        self.view = nil;
    }
}

- (void)data
{
    if (_isHeadRefreshing) {
       _nextUrl = kHealth;
        
    } else {
        
        if ([_nextUrl isEqualToString:@""]) {
            
            [self.tableView.footer noticeNoMoreData];
        }

    }
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:_nextUrl parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSDictionary *dic = responseObject[@"data"];
        
        NSDictionary *dict = dic[@"info"];
        
        _nextUrl = dict[@"next_url"];
        
        NSArray *array = dic[@"posts"];
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *dictt in array) {
            
            HealthModel *model = [[HealthModel alloc] init];
            [model setValuesForKeysWithDictionary:dictt];
            [tempArray addObject:model];
        }
        
        if (_isHeadRefreshing) {
            
            self.dataArray = tempArray;
            [self.tableView.header endRefreshing];
            [self.tableView reloadData];
        } else {
            
            [self.dataArray addObjectsFromArray:tempArray];
            [self.tableView.footer endRefreshing];
            [self.tableView reloadData];
        }


    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    HealthModel *model = self.dataArray[indexPath.row];
    
        if ([model.special_info[@"item_type"] isEqualToString:@"1"]) {
            
            HealthCell_01 *cell1 = [tableView dequeueReusableCellWithIdentifier:@"healthCell1" forIndexPath:indexPath];
            [cell1.photo sd_setImageWithURL:[NSURL URLWithString:model.auther[@"icon"]] placeholderImage:[UIImage imageNamed:@""]];
            cell1.nameLabel.text = model.auther[@"name"];
            cell1.nameLabel.font = [UIFont systemFontOfSize:14];
            cell1.timeLabel.font = [UIFont systemFontOfSize:12];
            cell1.contentLabel.font = [UIFont systemFontOfSize:14];
            cell1.contentLabel.numberOfLines = 0;
            cell1.timeLabel.text = [self timeStringWithTime:model.list_date];
            
            cell1.contentLabel.text = model.content;
            
            cell1.photo.layer.cornerRadius = cell1.photo.frame.size.width / 2;
            
            [cell1.picture sd_setImageWithURL:[NSURL URLWithString:model.thumbnail_medias.firstObject[@"url"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            
            cell1.numImage.image = [UIImage imageNamed:@"read"];
            
            cell1.numLabel.text = model.like_num;
            
            return cell1;
        } else if ([model.special_info[@"item_type"] isEqualToString:@"3"]) {
            
            HealthCell_02 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"healthCell2" forIndexPath:indexPath];
            
            cell2.photo.layer.cornerRadius = cell2.photo.frame.size.width / 2;
            
            cell2.nameLabel.font = [UIFont systemFontOfSize:14];
            cell2.timeLabel.font = [UIFont systemFontOfSize:12];
            cell2.contentLabel.font = [UIFont systemFontOfSize:14];
            cell2.contentLabel.numberOfLines = 0;
            
            [cell2.photo sd_setImageWithURL:[NSURL URLWithString:model.auther[@"icon"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell2.nameLabel.text = model.auther[@"name"] ;
            cell2.timeLabel.text = [self timeStringWithTime:model.list_date];
            
            NSString *url1 = model.thumbnail_medias[0][@"url"];
            
            [cell2.picture01 sd_setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            
            cell2.contentLabel.text = model.content;
            NSString *url2 = model.thumbnail_medias[1][@"url"];
            [cell2.picture02 sd_setImageWithURL:[NSURL URLWithString:url2] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell2.contentLabel.text = model.content;
            NSString *url3 = model.thumbnail_medias[2][@"url"];
            [cell2.picture03 sd_setImageWithURL:[NSURL URLWithString:url3] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell2.contentLabel.text = model.content;
            
            
            
            
            cell2.numImage.image = [UIImage imageNamed:@"read"];
            cell2.numLabel.text = model.like_num;
            
            return cell2;
            
        } else if ([model.special_info[@"item_type"] isEqualToString:@"2"]) {
            
            HealthCell_03 *cell3 = [tableView dequeueReusableCellWithIdentifier:@"healthCell3" forIndexPath:indexPath];
            cell3.photo.layer.cornerRadius = cell3.photo.frame.size.width / 2;
            
            cell3.nameLabel.font = [UIFont systemFontOfSize:14];
            cell3.timeLabel.font = [UIFont systemFontOfSize:12];
            cell3.contentLabel.font = [UIFont systemFontOfSize:14];
            cell3.contentLabel.numberOfLines = 0;
            
            [cell3.photo sd_setImageWithURL:[NSURL URLWithString:model.auther[@"icon"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell3.nameLabel.text = model.auther[@"name"] ;
            cell3.timeLabel.text = [self timeStringWithTime:model.list_date];
            cell3.contentLabel.text = model.content;
            
            NSString *url1 = model.thumbnail_medias[0][@"url"];
            [cell3.picture01 sd_setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            NSString *url2 = model.thumbnail_medias[1][@"url"];
            [cell3.piture02 sd_setImageWithURL:[NSURL URLWithString:url2] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            
            cell3.numImage.image = [UIImage imageNamed:@"read"];
            cell3.numLabel.text = model.like_num;
            
            
            
            return cell3;
            
        } else if ([model.special_info[@"item_type"] isEqualToString:@"1"] && [model.content isEqualToString:@""]) {
            
            HealthCell_05 *cell5 = [tableView dequeueReusableCellWithIdentifier:@"healthCell5" forIndexPath:indexPath];
            
            cell5.photo.layer.cornerRadius = cell5.photo.frame.size.width / 2;
            
            cell5.nameLabel.font = [UIFont systemFontOfSize:14];
            cell5.timeLabel.font = [UIFont systemFontOfSize:12];
            
            [cell5.photo sd_setImageWithURL:[NSURL URLWithString:model.auther[@"icon"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell5.nameLabel.text = model.auther[@"name"] ;
            cell5.timeLabel.text = [self timeStringWithTime:model.list_date];
            
            [cell5.pitureImage sd_setImageWithURL:[NSURL URLWithString:model.m_url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell5.numImage.image = [UIImage imageNamed:@"read"];
            cell5.numLabel.text = model.like_num;
            
            
            return cell5;
            
        } else {
            
            HealthCell_04 *cell4 = [tableView dequeueReusableCellWithIdentifier:@"healthCell4" forIndexPath:indexPath];
            
            cell4.photo.layer.cornerRadius = cell4.photo.frame.size.width / 2;
            
            cell4.nameLabel.font = [UIFont systemFontOfSize:14];
            cell4.timeLabel.font = [UIFont systemFontOfSize:12];
            cell4.contentLabel.font = [UIFont systemFontOfSize:14];
            cell4.contentLabel.numberOfLines = 0;
            
            [cell4.photo sd_setImageWithURL:[NSURL URLWithString:model.auther[@"icon"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell4.nameLabel.text = model.auther[@"name"] ;
            cell4.timeLabel.text = [self timeStringWithTime:model.list_date];
            
            cell4.contentLabel.text = model.content;
            cell4.numImage.image = [UIImage imageNamed:@"read"];
            cell4.numLabel.text = model.like_num;
            
            return cell4;
            
        }

    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HealthModel *model = self.dataArray[indexPath.row];
    
    if ([model.special_info[@"item_type"] isEqualToString:@"1"]) {
        
        
        return 400;
        
    } else if ([model.special_info[@"item_type"] isEqualToString:@"3"]) {
        
        
        return 304;
        
    } else if ([model.special_info[@"item_type"] isEqualToString:@"2"]) {
        
        return 304;
        
    } else if ([model.special_info[@"item_type"] isEqualToString:@"1"] && [model.content isEqualToString:@"placeholder"]) {
        
        
        return 354;
        
    } else {
        
        
        return 204;
        
    }

    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    EnjoyDetailViewController *enjoyVC = [[EnjoyDetailViewController alloc] init];
    
    HealthModel *model = self.dataArray[indexPath.row];
    
    enjoyVC.url = model.weburl;
    
    if (model.thumbnail_medias.count > 0) {
    enjoyVC.pic_url = model.thumbnail_medias[0][@"url"];
    }
    
    enjoyVC.newsTitle = model.auther[@"name"];
    enjoyVC.tag = @"2";
    [self.navigationController pushViewController:enjoyVC animated:YES];
    
}


// 时间转换
- (NSString *)timeStringWithTime:(NSString *)time
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *data = [formatter dateFromString:time];
    
    return [data timeIntervalDescription];
}
#pragma mark - 刷新数据
- (void)setUpRefresh
{
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(headRefresh)];
    [self.tableView.header beginRefreshing];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(footRefresh)];
}


- (void)headRefresh
{
   
       _isHeadRefreshing = YES;
        
        [self data];
    
}
- (void)footRefresh
{
    _isHeadRefreshing = NO;
    [self data];
   
    
}

@end
